




//
//  Homeview.swift
//  mywei
//
//  Created by myd on 15/12/15.
//  Copyright © 2015年 myd1. All rights reserved.
//

import UIKit

protocol chilklazhomebutton : NSObjectProtocol
{
    func chilklogin()
    func chilkzhuce()
}
class Homeview: UIView {

    
  weak   var delega : chilklazhomebutton?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
         self.backgroundColor=UIColor(white: 237/255.0, alpha: 1)
     prepareUI()
    }
func start()
{

    // 获取暂停时间
    let pauseTime = icon.layer.timeOffset
    // 设置动画速度为1
    icon.layer.speed = 1
    
    icon.layer.timeOffset = 0
    
    icon.layer.beginTime = 0
    
    let timeSincePause = icon.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
    
    icon.layer.beginTime = timeSincePause
    }
    func pach()
    {
        // 记录暂停时间
        let pauseTime = icon.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        icon.layer.speed = 0
        
        // 设置动画偏移时间
        icon.layer.timeOffset = pauseTime
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setcontroller(title : String , image : String)
    {
   icon.hidden = true
        mess.text=title
        house.image=UIImage(named: image)
    }
    func startan()
    {
   icon.hidden = false
    let an = CABasicAnimation(keyPath: "transform.rotation")
        an.duration=20
        an.repeatCount = MAXFLOAT
        an .toValue = 2*M_PI
        an.removedOnCompletion=false
        icon.layer.addAnimation(an, forKey: "transform.rotation")
        
    }
    
    private func prepareUI()
    {
       
       self.addSubview(icon)
         self.addSubview(backview)
        self.addSubview(house)
        self.addSubview(mess)
        self.addSubview(zhuce)
        self.addSubview(login)
        backview.translatesAutoresizingMaskIntoConstraints=false
        icon.translatesAutoresizingMaskIntoConstraints=false
        house.translatesAutoresizingMaskIntoConstraints=false
        mess.translatesAutoresizingMaskIntoConstraints=false
        zhuce.translatesAutoresizingMaskIntoConstraints=false
        login.translatesAutoresizingMaskIntoConstraints=false
        self.addConstraint(NSLayoutConstraint(item:icon, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item:icon, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        self.addConstraint(NSLayoutConstraint(item:house, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: icon, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item:house, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: icon, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant:0))
        
        self.addConstraint(NSLayoutConstraint(item: mess, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: house, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: mess, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: icon, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        self.addConstraint(NSLayoutConstraint(item: mess, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 220))
        
        self.addConstraint(NSLayoutConstraint(item: zhuce, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: mess, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: zhuce, attribute:NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: mess, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: zhuce, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: zhuce, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 35))
        
        self.addConstraint(NSLayoutConstraint(item: login, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: zhuce, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: login, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem:mess, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: login, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: zhuce, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: login, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: zhuce, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
     
        self.addConstraint(NSLayoutConstraint(item: backview, attribute: NSLayoutAttribute.Leading, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: backview, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: backview, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem:house, attribute:NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
    }
    
    //转轮
    private lazy var icon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //房子
     private lazy var house = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息
    private lazy var message = UIImageView(image: UIImage(named: "visitordiscover_image_message"))
    //我
    private lazy var main1 = UIImageView(image: UIImage(named: "visitordiscover_image_profile"))
    private lazy var backview = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var mess : UILabel = {
   let title=UILabel()
        title.text="关注一些人,看看有什么惊喜"
        title.textColor=UIColor.grayColor()
        title.numberOfLines=0
        return title
    }()
    private lazy var zhuce : UIButton = {
    let zhu = UIButton()
        zhu .setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        zhu.setTitle("注册", forState: UIControlState.Normal)
        zhu.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        zhu.addTarget(self, action: "zhuce1", forControlEvents: UIControlEvents.TouchUpInside);
        return zhu
    }()
    private lazy var login : UIButton = {
   let login = UIButton()
        login .setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        login.setTitle("登陆", forState: UIControlState.Normal)
        login.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        login.addTarget(self, action: "login1", forControlEvents: UIControlEvents.TouchUpInside);
        return login
    }()
 @objc private  func login1()
    {
      delega?.chilklogin()
    }
 @objc private func zhuce1()
  {
   delega?.chilkzhuce()
    }
}

